package com.ckx.controller;

import com.alibaba.nacos.common.model.RestResult;
import com.ckx.dao.UserMapper;
import com.ckx.dao.entity.UserDO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.data.id.IdUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;

    @GetMapping("add")
    public RestResult<Long> add(Long id) {
        UserDO userDO = new UserDO();
        userDO.setId(id == null ? IdUtil.getSnowflakeNextId() : id);
        userDO.setUserName(IdUtil.fastSimpleUUID());
        userDO.setAddress("ddddddere");
        userDO.setAge(45);
        userMapper.insert(userDO);
        return RestResult.<Long>builder().withData(userDO.getId()).build();
    }

    @GetMapping("get")
    public RestResult<UserDO> get(Long id) throws Exception {
        UserDO userDO = userMapper.selectByPrimaryKey(id);
        return RestResult.<UserDO>builder().withData(userDO).build();
    }

    @GetMapping("list")
    public RestResult<List<UserDO>> list(UserDO userDO) {
        List<UserDO> userList = userMapper.selectList(userDO);
        return RestResult.<List<UserDO>>builder().withData(userList).build();
    }

}
